//
//  ActiveViewController.m
//  Kfangchan
//
//  Created by liran on 14/11/15.
//  Copyright (c) 2014年 Beijing Renjuren Network Technology Co., Ltd. All rights reserved.
//

#import "ActiveViewController.h"
#import "ActiveTableViewCell.h"

@interface ActiveViewController ()

@property (strong, nonatomic) IBOutlet UITableView *activeTableView;

@end

@implementation ActiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.titleView = [[Theam currentTheam] navigationTitleViewWithTitle:@"活动喜报"];
    self.navigationItem.leftBarButtonItem = [[Theam currentTheam] navigationBarLeftButtonItemWithImage:[UIImage imageNamed:@"nav_back.png"] Target:self Selector:@selector(back)];
    
    self.activeTableView.size = CGSizeMake(self.activeTableView.width, [UIScreen mainScreen].bounds.size.height - 80);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableView
#pragma mark 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.list count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    BuildingActive *active = self.list[indexPath.row];
    return 80 + active.webViewHeight;
}

#pragma mark 设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"ActiveTableViewCell";
    
    BuildingActive *active = self.list[indexPath.row];
    
    ActiveTableViewCell *cell = (ActiveTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        NSArray *cells = [[NSBundle mainBundle] loadNibNamed:identifier owner:self options:nil];
        cell = [cells objectAtIndex:0];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    cell.descrWebView.delegate = self;
    cell.descrWebView.tag = indexPath.row;

    [cell initCell:active];
    if (active.isLoad) {
        [cell resizeCell];
    }
    
    return cell;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    int row = (int)webView.tag;
    BuildingActive *active = [self.list objectAtIndex:row];
    if (active.isLoad) {
        return;
    }
    
    NSString *height_str= [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"];
    int webHeight = [height_str intValue];
    if (webHeight < 10) {
        webHeight = 10;
    }
    
    
    active.isLoad = YES;
    active.webViewHeight = webHeight;
    [self.list replaceObjectAtIndex:row withObject:active];
    
    [self.activeTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:row inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark -
#pragma mark
- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
